/****************************************************************************
*  Title: monitor.sc
*  Author: Bryant Smith
*  Date: 04/16/2011
*  Description: monitor test bench to write output to file
****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define IMG_ROWS 480
#define IMG_CLMS 640

#define TEST_IMG_COUNT 8

import "c_queue";


behavior monitor1(i_receiver OutQueue)
{

	// File names
	char OUTPUT_BMP[] = "output/outputX.bmp"; // output file name
	
	// image buffers these are currently dependent on the size and format of the image used
	unsigned char row_buffer_1[IMG_CLMS];
	unsigned char image_buffer[IMG_ROWS][IMG_CLMS]; //char image buffer
	char imgBufCount[1];
	
	int row, wt_cnt, i;
		
	void main(void) {
		
		//File variable
		FILE * wptr;
		
		for( i = 1; i <= TEST_IMG_COUNT; i++)
		{
			// Determine Next image
			sprintf(imgBufCount, "%d", i);
			OUTPUT_BMP[13] = imgBufCount[0];
		
			// open output file file
			wptr = fopen(OUTPUT_BMP, "a"); // append to file which already has header from stimulus

			for(row = 0; row < IMG_ROWS; row++)
			{
				// Load buffer with a row of the image
				OutQueue.receive(row_buffer_1, (IMG_CLMS));
				//printf("Monitor read row %d from queue\n", row);
				
				for(wt_cnt = 0; wt_cnt < (IMG_CLMS); wt_cnt++)
				{

					fprintf(wptr,"%c",row_buffer_1[wt_cnt]);
				}
			}
			printf("Monitor - Writing Image %d\n", i);
			fclose(wptr);

		}
		
  
	
	printf("Processing completed check file %s\n", OUTPUT_BMP);
	
	exit(0);
	}
};


// reads queue for lane angle and prints out
behavior monitor2(i_receiver LaneOutQueue)
{

	// File names
	char OUTPUT_LANE[] = "output/LaneResults.txt"; // output file name
	int DATA[1];
	int count;
	int LaneDegree;
	
		
	void main(void) {
	
		//File variable
		FILE * wptr;
		wptr = fopen(OUTPUT_LANE, "w");
		count = 1;
	
		while(1)
		{
			// read data from the queue and write it out
			LaneOutQueue.receive(DATA, 1);
			
			// convert queue unsigned int to signed.  for accurate degree
			LaneDegree = DATA[0] - 256;
			fprintf(wptr, "Image %d lane angel: %d\n", count, LaneDegree);
			

			
			count ++;		
		}
	}
};

behavior monitor(i_receiver OutQueue, i_receiver LaneOutQueue)
{
	monitor1  mon1(OutQueue);
	monitor2  mon2(LaneOutQueue);
	
	void main() {
		par {
			mon1.main();
			mon2.main();
		}
	}
};

